import { Controller, Get, Post, Body } from '@nestjs/common';
import { LoggingService } from '@/services/logging.service';
import { AutoContext, AutoTraceId, AutoUri } from '@/decorators/auto-context.decorator';
import { getCurrentContext, getCurrentTraceId, getCurrentUri } from '@/common/async-context';
import { ResponseUtil } from '@/common/utils/response.util';

@Controller('example')
export class ExampleController {
    constructor(private readonly loggingService: LoggingService) {}

    @Get('context')
    async getContext(@AutoContext() context: any) {
        // 使用装饰器自动获取上下文
        this.loggingService.business('获取上下文信息', context);

        // 返回标准响应格式
        return ResponseUtil.success(context, '上下文信息获取成功');
    }

    @Get('trace-id')
    async getTraceId(@AutoTraceId() traceId: string) {
        // 只获取 traceId
        this.loggingService.app('获取 traceId', { traceId });

        // 返回标准响应格式
        return ResponseUtil.success({ traceId }, 'TraceId 获取成功');
    }

    @Get('uri')
    async getUri(@AutoUri() uri: string) {
        // 只获取 URI
        this.loggingService.app('获取 URI', { uri });

        // 返回标准响应格式
        return ResponseUtil.success({ uri }, 'URI 获取成功');
    }

    @Post('log-test')
    async logTest(@Body() data: any) {
        // 测试不同类型的日志
        this.loggingService.app('应用日志测试', data);
        this.loggingService.business('业务日志测试', data);
        this.loggingService.database('数据库日志测试');
        this.loggingService.cache('缓存日志测试');
        this.loggingService.http('HTTP 日志测试');
        this.loggingService.perf('测试操作', data);

        // 获取当前上下文
        const currentContext = getCurrentContext();
        const currentTraceId = getCurrentTraceId();
        const currentUri = getCurrentUri();

        // 返回标准响应格式
        return ResponseUtil.success(
            {
                data,
                context: {
                    current: currentContext,
                    traceId: currentTraceId,
                    uri: currentUri,
                },
            },
            '日志测试完成',
        );
    }

    @Get('paginated-example')
    async getPaginatedExample() {
        // 模拟分页数据
        const mockData = [
            { id: 1, name: 'Item 1' },
            { id: 2, name: 'Item 2' },
            { id: 3, name: 'Item 3' },
        ];

        // 返回分页响应格式
        return ResponseUtil.paginated(mockData, 1, 10, 25, '分页数据success');
    }

    @Get('custom-response')
    async getCustomResponse() {
        // 返回自定义响应格式
        return ResponseUtil.custom(201, '自定义响应消息', { custom: 'data' });
    }
}
